package com.bocai.freeman.view.activitys;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseActivity;
import com.bocai.freeman.view.fragments.ContactFrag;
import com.bocai.freeman.view.fragments.HomeFrag;
import com.bocai.freeman.view.fragments.LatelyFrag;
import com.bocai.freeman.view.fragments.MeFrag;
import com.bocai.freeman.view.fragments.OrderFrag;
import com.trello.rxlifecycle.components.support.RxFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HomeActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_home)
    ImageView ivHome;
    @Bind(R.id.tv_home)
    TextView tvHome;
    @Bind(R.id.ll_home)
    LinearLayout llHome;
    @Bind(R.id.iv_lately)
    ImageView ivLately;
    @Bind(R.id.tv_lately)
    TextView tvLately;
    @Bind(R.id.ll_lately)
    LinearLayout llLately;
    @Bind(R.id.iv_contacts)
    ImageView ivContacts;
    @Bind(R.id.tv_contacts)
    TextView tvContacts;

    @Bind(R.id.iv_order)
    ImageView ivOrder;
    @Bind(R.id.tv_order)
    TextView tvOrder;
    @Bind(R.id.ll_order)
    LinearLayout llOrder;
    @Bind(R.id.iv_me)
    ImageView ivMe;
    @Bind(R.id.tv_me)
    TextView tvMe;
    @Bind(R.id.ll_me)
    LinearLayout llMe;
    @Bind(R.id.ll_contact)
    LinearLayout llContact;
    private RxFragment mFragHome;
    private RxFragment mFragMe;
    private RxFragment mFragOrder;
    private RxFragment mFragContact;
    private RxFragment mFragLately;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        setSelect(0);
        initEvent();
    }

    @Override
    protected void initEvent() {
        llHome.setOnClickListener(this);
        llMe.setOnClickListener(this);
        llLately.setOnClickListener(this);
        llOrder.setOnClickListener(this);
        llContact.setOnClickListener(this);
    }

    public void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        switch (i) {
            case 0:
                resetImg();
                hideFragment(transaction);
                if (mFragHome == null) {
                    mFragHome = new HomeFrag();
                    transaction.add(R.id.fl_home, mFragHome);
                } else {
                    transaction.show(mFragHome);
                }
                tvHome.setTextColor(getResources().getColor(R.color.cl_primary));
                ivHome.setImageResource(R.mipmap.home);
                break;
            case 1:
                resetImg();
                hideFragment(transaction);
                if (mFragLately == null) {
                    mFragLately = new LatelyFrag();
                    transaction.add(R.id.fl_home, mFragLately);
                } else {
                    transaction.show(mFragLately);
                }
                tvLately.setTextColor(getResources().getColor(R.color.cl_primary));
                ivLately.setImageResource(R.mipmap.recently);
                break;
            case 2:
                resetImg();
                hideFragment(transaction);
                if (mFragContact == null) {
                    mFragContact = new ContactFrag();
                    transaction.add(R.id.fl_home, mFragContact);
                } else {
                    transaction.show(mFragContact);
                }
                tvContacts.setTextColor(getResources().getColor(R.color.cl_primary));
                ivContacts.setImageResource(R.mipmap.renmai);
                break;
            case 3:
                resetImg();
                hideFragment(transaction);
                if (mFragOrder == null) {
                    mFragOrder = new OrderFrag();
                    transaction.add(R.id.fl_home, mFragOrder);
                } else {
                    transaction.show(mFragOrder);
                }
                tvOrder.setTextColor(getResources().getColor(R.color.cl_primary));
                ivOrder.setImageResource(R.mipmap.order);
                break;
            case 4:
                resetImg();
                hideFragment(transaction);
                if (mFragMe == null) {
                    mFragMe = new MeFrag();
                    transaction.add(R.id.fl_home, mFragMe);
                } else {
                    transaction.show(mFragMe);
                }
                tvMe.setTextColor(getResources().getColor(R.color.cl_primary));
                ivMe.setImageResource(R.mipmap.my);
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    private void resetImg() {
        tvHome.setTextColor(getResources().getColor(R.color.cl_textcolorcommon));
        ivHome.setImageResource(R.mipmap.unhome);
        tvLately.setTextColor(getResources().getColor(R.color.cl_textcolorcommon));
        ivLately.setImageResource(R.mipmap.unrecently);
        tvContacts.setTextColor(getResources().getColor(R.color.cl_textcolorcommon));
        ivContacts.setImageResource(R.mipmap.unrenmai);
        tvOrder.setTextColor(getResources().getColor(R.color.cl_textcolorcommon));
        ivOrder.setImageResource(R.mipmap.unorder);
        tvMe.setTextColor(getResources().getColor(R.color.cl_textcolorcommon));
        ivMe.setImageResource(R.mipmap.unmy);


    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFragHome != null) {
            transaction.hide(mFragHome);
        }
        if (mFragLately != null) {
            transaction.hide(mFragLately);
        }
        if (mFragContact != null) {
            transaction.hide(mFragContact);
        }
        if (mFragOrder != null) {
            transaction.hide(mFragOrder);
        }
        if (mFragMe != null) {
            transaction.hide(mFragMe);
        }


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ll_home:
                setSelect(0);
                break;
            case R.id.ll_lately:
                setSelect(1);
                break;
            case R.id.ll_contact:
                setSelect(2);
                break;
            case R.id.ll_order:
                setSelect(3);
                break;
            case R.id.ll_me:
                setSelect(4);
                break;
        }
    }

    @Override
    public void setData(Object obj) {
        
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
